<template>
	<view class="app">
		<view class="price-box">
			<text>支付金额</text>
			<text class="price">￥{{info.order.price||''}}</text>
		</view>
		<view class="text-center">
			<text>现金支付：</text>
			<text class="price">￥{{Number(info.order.money||0).toFixed(2)}}</text>
		</view>
		<view class="text-center">
			<text>米券支付：</text>
			<text class="price">{{info.order.micredit||0}}</text>
		</view>
		<view class="miNum">当前米券余额：{{info.totalmicredit||0}}</view>
		<text class="mix-btn" @click="confirm">确认支付</text>
		<uni-popup ref="bind" type="dialog" :is-mask-click="false">
			<view class="nobind">
				<view>获取更多折扣，请完善联系方式</view>
				<button class="l_w_login" open-type="getPhoneNumber" @getphonenumber="getUserProfile">一键绑定手机号</button>
			</view>
		</uni-popup>
		<!-- 输入框示例 -->
		<uni-popup ref="tixian" type="dialog">
			<uni-popup-dialog type="info" cancelText="关闭" before-close="true" confirmText="确定" title="提现密码"
				content="欢迎使用 uni-popup!" @close="closeDia" @confirm="realSubmit">
				<input type="password" v-model="password" class="with_password" placeholder="请输入提现密码">
			</uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import {
		throttle
	} from 'lodash'
	export default {
		data() {
			return {
				id: "",
				orderid: '',
				info: {
					order: {}
				},
				isGroupBuy: 0, //1团购 2次卡
				password: '',
				isplay: false,
				type: '',
				optionBody: {}
			};
		},
		onShow() {
			this.password = ''
		},
		onLoad(options) {
			this.optionBody = options
			this.orderid = options.orderid;
			if (options.isGroupBuy) {
				this.isGroupBuy = options.isGroupBuy;
			}
			if (options.type) {
				this.type = options.type;
			}
			this.getPayMain()
		},
		methods: {
			// 获取订单信息
			async getPayMain() {
				let res = await this.$axios('Pay/main', "POST", 'order', {
					orderid: this.orderid,
					apptype: 3,
					code: '',
					iswx: 1
				})
				if (res.data.code == 200) {
					this.info = res.data.data || this.info;
				}
			},
			//确认支付
			confirm() {
				if (this.info.order.money == 0) {
					this.getPayWord();
					return;
				} else {
					this.realSubmit();
				}
			},
			closeDia() {
				this.password = '';
				this.$refs.tixian.close();
			},
			getPayWord() {
				this.$axios('Info/paymain', "POST", 'member').then(res => {
					if (res.data.code = 200) {
						if (res.data.data.paypwd == '') {
							this.$api.msg('请先设置支付密码');
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/user/paypassword'
								})
							}, 1000);
							return;
						} else {
							this.$refs.tixian.open();
						}
					}
				})
			},
			realSubmit: throttle(async function() {
				// #ifdef MP-WEIXIN
				let status = 1;
				let apptype = 3;
				// #endif
				// #ifdef MP-ALIPAY
				let status = 2;
				let apptype = 3;
				// #endif
				// #ifdef H5
				let status = 2;
				let apptype = 1;
				// #endif
				if (this.info.order.money == 0) {
					status = 3;
				}
				if (this.info.order.money == 0 && this.password == '') {
					return this.$api.msg('请输入提现密码');
				}
				this.$refs.tixian.close();
				if (this.type && !this.optionBody.leader_member_id && !this.optionBody.date_card_id) {
					const resJoinGroup = await this.$axios('Group/joinGroup', "POST", 'pintuan', {
						...this.optionBody
					})
					if (resJoinGroup.data.code != 200) {
						uni.hideLoading()
						this.$api.msg(resJoinGroup.data.data)
						return
					}
				}
				if (this.type && this.optionBody.leader_member_id && !this.optionBody.date_card_id) {
					const resJoinGroup = await this.$axios('Group/joinGroupMulti', "POST", 'pintuan', {
						...this.optionBody
					})
					if (resJoinGroup.data.code != 200) {
						uni.hideLoading()
						this.$api.msg(resJoinGroup.data.data)
						return
					}
				}
				this.$axios('Pay/goPay', "GET", 'pt', {
					type: status,
					ordersn: this.info.order.ordersn,
					apptype: apptype,
					paypwd: this.password
				}).then(async res => {
					this.password = '';
					if (res.data.code == 200) {
						// if(res.data.data.sign){
						if (status == 1) {
							uni.requestPayment({
								provider: 'wxpay',
								timeStamp: res.data.data.timeStamp,
								nonceStr: res.data.data.nonceStr,
								package: res.data.data.package,
								signType: 'MD5',
								paySign: res.data.data.sign,
								success: res => {
									this.$api.msg('支付成功');
									this.successUrl(true);
								},
								fail: () => {
									this.$api.msg('支付失败');
								}
							});
						} else if (status == 2) {
							if (apptype == 1) {
								window.location.href =
									'https://pintuan.jinghushi.com/pay.htm?goto=' + res.data.data
							} else if (apptype == 3) {
								my.tradePay({
									tradeNO: res.data.data.trade_no,
									success: res => {
										if (res.resultCode == 9000) {
											this.$api.msg('支付成功');
											this.successUrl(true);
										} else {
											this.$api.msg(res.memo);
										}
									},
									fail: error => {
										this.$api.msg('支付失败');
									},
								});
							}
						} else {
							this.$api.msg('支付成功');
							this.successUrl(false);
						}
					}
				})
			}, 5000, {
				leading: true,
				trailing: false
			}),
			successUrl(isBind) {
				const type = this.optionBody.type == 'pintuan' ? 1 : ''
				if (!isBind) {
					this.$navRed(`/pages/user/order?type=${type}`);
				} else {
					this.getBindPhone();
				}
			},
			// 是否绑定手机号
			getBindPhone() {
				this.$axios('Info/member_info', "POST", 'member').then(res => {
					if (res.data.code = 200) {
						if (res.data.data.member.mobile == '' || res.data.data.member.mobile.length < 1 || res.data
							.data.member.mobile === null || res.data.data.member.mobile == undefined) {
							this.$refs.bind.open();
						} else {
							this.$navHome('/pages/index/order?type=' + this.type);
						}
					}
				})
			},
			getUserProfile(e) {
				let that = this;
				// #ifdef MP-WEIXIN
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						let obj = e.detail;
						obj.code1 = loginRes.code;
						that.$axios('bindmobile/bmobile', "POST", 'member', obj).then(res => {
							if (res.data.code == 200) {
								that.$refs.bind.close();
								this.$navHome('/pages/index/order?type=' + this.type);
							}
						})
					},
					fail() {
						that.$api.msg('绑定失败，请重试！')
					}
				})
				// #endif
				// #ifdef MP-ALIPAY
				my.getPhoneNumber({
					success: (res) => {
						let encryptedData = res.response;
						that.$axios('bindmobile/bmobilealipay', "POST", 'member', {
							code: JSON.parse(encryptedData).response
						}).then(res => {
							if (res.data.code == 200) {
								that.$refs.bind.close();
								this.$navHome('/pages/index/order?type=' + this.type);
							}
						})
					},
					fail: (res) => {
						console.log(res);
						that.$api.msg('绑定失败，请重试！')
					},
				});
				// #endif
			},
		}
	}
</script>

<style lang='scss'>
	page {
		background: #fff;
	}

	.app {
		width: 100%;
		padding: 0 20rpx;
	}

	.text-center {
		margin-top: 26rpx;
		color: #333;
		font-size: 30rpx;

		.price {
			color: $bg-color;
			font-weight: bold;
			font-size: 32rpx;
		}
	}

	.miNum {
		color: #666;
		margin-top: 60rpx;
	}

	.price-box {
		background-color: #fff;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		font-size: 28upx;
		color: #909399;

		.price {
			font-size: 50upx;
			color: #303133;
			margin-top: 12upx;

			/* &:before {
				content: '￥';
				font-size: 40upx;
			} */
		}

		.p_total {
			font-size: 30upx;
			color: $base-color;
			margin-top: 10upx;
		}

	}

	.mix-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 630upx;
		height: 80upx;
		margin: 80upx auto 30upx;
		font-size: $font-lg;
		color: #fff;
		background-color: $bg-color;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(96, 43, 154, 0.4);
	}
</style>